package hot100;

public class Lc34 {
    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[2];
        if (binarySearchEQ(nums, target) < 0) {
            ret[0] = -1;
            ret[1] = -1;
            return ret;
        }
        ret[0] = binarySearchGT(nums, target - 1);
        ret[1] = binarySearchGT(nums, target) - 1;
        return ret;
    }

    int binarySearchGT(int[] nums, int target) {
        int left = -1;
        int right = nums.length;
        while (left + 1 < right) {
            int mid = (right - left) / 2 + left;
            if (nums[mid] > target) {
                right = mid;
            }
            else {
                left = mid;
            }
        }
        return right;
    }
    int binarySearchEQ(int[] nums, int target) {
        int left = -1;
        int right = nums.length;
        while (left + 1 < right) {
            int mid = (right - left) / 2 + left;
            if (nums[mid] > target) {
                right = mid;
            }
            else if (nums[mid] < target) {
                left = mid;
            }
            else {
                return mid;
            }
        }
        return -right - 1;
    }


}
